package com.daylightrobbery.server.packet.game;

import com.daylightrobbery.server.GameManager;
import com.daylightrobbery.server.entity.PlayerInfo;
import com.daylightrobbery.server.packet.game.common.RandomUtils;
import com.daylightrobbery.server.packet.gameobjects.TreasureBoxDescription;
import com.daylightrobbery.server.packet.type.DataPacket;
import com.daylightrobbery.server.packet.type.RoomDataPacket;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;

@Component
public class RoundFinishedPacket implements RoomDataPacket {

    private String uid;
    private int roomId;
    private int type = 30004;

    private static final Logger logger = LoggerFactory.getLogger(RoundFinishedPacket.class);

    @JsonIgnoreProperties(ignoreUnknown = true)
    private HashMap<Integer, TreasureBoxDescription> treasureBoxDescMap; //新生成的箱子

    @Override
    public DataPacket process() {
        ConcurrentHashMap<String, PlayerInfo> players = GameManager.getInstance().getRoomById(roomId).getPlayers();
        logger.info("[回合结束]当前结束回合用户 account: " + players.get(this.uid).getAccount());
        List<String> playerUidRoundOrder = GameManager.getInstance().getPlayerUidRoundOrder(roomId);
        for (int i = 0; i < playerUidRoundOrder.size(); i++) {
            if (uid.equals(playerUidRoundOrder.get(i))) {
                this.uid = playerUidRoundOrder.get(i + 1 >= playerUidRoundOrder.size() ? 0 : i + 1);
                logger.info("[回合切换]下一回合用户 account: " + players.get(this.uid).getAccount());
                break;
            }
        }

        while (RandomUtils.getRandomResult(0.50f)) {
            if (treasureBoxDescMap == null) {
                treasureBoxDescMap = new HashMap<>();
            }
            int level = ThreadLocalRandom.current().nextInt(1, 5);
            TreasureBoxDescription tb = TreasureBoxDescription.GetRandomTB(level);
            int randomPathIndex = ThreadLocalRandom.current().nextInt(0, 10000);
            this.treasureBoxDescMap.put(randomPathIndex, tb);
            logger.info("生成箱子...");
        }

        String nextUid = this.uid;
        Integer roomId = GameManager.getInstance().getPlayerInGameRoomId(nextUid);
        GameManager.getInstance().getRoomById(roomId).getPacket().setCurrentTurnPlayerUid(nextUid);
        return this;
    }

    public HashMap<Integer, TreasureBoxDescription> getTreasureBoxDescMap() {
        return treasureBoxDescMap;
    }

    public RoundFinishedPacket setTreasureBoxDescMap(HashMap<Integer, TreasureBoxDescription> treasureBoxDescMap) {
        this.treasureBoxDescMap = treasureBoxDescMap;
        return this;
    }

    public String getUid() {
        return uid;
    }

    public RoundFinishedPacket setUid(String uid) {
        this.uid = uid;
        return this;
    }

    public RoundFinishedPacket setRoomId(int roomId) {
        this.roomId = roomId;
        return this;
    }

    @Override
    public int getType() {
        return type;
    }

    @Override
    public int getRoomId() {
        return roomId;
    }
}
